//
//  ProfileView.swift
//  loveHHEveryDay
//
//  Created by yw on 2025/4/2.
//

import SwiftUI

class PathManager:ObservableObject{
    @Published var path = NavigationPath()
}

struct ProfileView: View {
    @AppStorage(kUserDateIcon) var icon:String = ""
    @AppStorage(kUserDateName) var name:String = ""
    @AppStorage(kUserDateId) var userId:String = ""
    //    @State var path = NavigationPath()
    @StateObject var pathManager = PathManager()
    @AppStorage(kUserIconBase64) var iconBase64:String = ""
    @State var list:[[String:String]] = [
        
        
        [
            "title":"账号管理",
            "icon":"person.fill",
            "type":"push",
            "value":"",
        ],
        [
            "title":"用户绑定",
            "icon":"plus.app.fill",
            "type":"push",
            "value":"",
        ],
        [
            "title":"分享App",
            "icon":"square.and.arrow.up.fill",
            "type":"",
            "value":"",
        ],
        [
            "title":"建议反馈",
            "icon":"pencil.circle.fill",
            "type":"",
            "value":"",
        ],
        [
            "title":"给个好评",
            "icon":"heart.fill",
            "type":"",
            "value":"",
        ],
        [
            "title":"关于我们",
            "icon":"exclamationmark.circle.fill",
            "type":"push",
            "value":"",
        ],
        [
            "title":"更多App",
            "icon":"ellipsis.circle.fill",
            "type":"push",
            "value":"",
        ],
        
    ]
    var body: some View {
        NavigationStack(path:$pathManager.path) {
            VStack {
                ScrollView(showsIndicators: false) {
                    VStack {
                        iconView
                        listView
                    }
                }
                .navigationDestination(for: String.self) { item in
                    if item == "关于我们" {
                        AboutView()
                    } else if item == "账号管理" {
                        AccountSettingView()
                    } else if item == "更多App" {
                        MoreAppView()
                    } else if item == "用户绑定" {
                        BindUserIdView()
                    } else if item == "修改名称" {
                        ChangeUserName()
                    } else if item == "修改背景" {
                        ChangeBackgroundView()
                    }
                }
                
            }.navigationTitle("我的")
                .navigationBarTitleDisplayMode(.inline)
                .frame(maxWidth: .infinity)
                .background(Color("color_bg"))
                
        }.environmentObject(pathManager)
        
        
        //        }
    }
    
    var iconView:some View {
        
        ZStack{
            ZStack {
                RoundedRectangle(cornerRadius: 20)
                    .fill(.white)
                    .frame(maxWidth: .infinity)
                    .frame(height: 180)
                    .padding(.horizontal,20)
                    .offset(y: 10)
                VStack {
                    NavigationLink {
                        AccountSettingView()
                    } label: {
                       
                        Image(uiImage: ((iconBase64.imageFromBase64) ?? UIImage(named: "wel_boy"))!)
                                    .resizable()
                                    .frame(width:108, height:108)
                                    .clipShape(Circle())
                                    .aspectRatio(contentMode: .fill)
                                    .padding(4)
                                    .background(.colorWelBg01)
                                    .clipShape(Circle())
                                    .padding(.top,20)
                        
                    }
                    
                    Text(name).modifier(YWTextModifer(fontSize: 20, fontWeight: .bold, design: .rounded, color: Color("color_text_main")))
                        .padding(.top,6)
                    HStack {
                        Image("profile_id")
                            .resizable()
                            .frame(width: 30, height: 30)
                            .aspectRatio(contentMode: .fit)
                        Spacer()
                        //                    HStack {
                        Text(userId).modifier(YWTextModifer(fontSize: 12, fontWeight: .regular, design: .rounded, color:Color("color_text_main")))
                            .padding(.top,2)
                        Spacer()
                        Image(systemName: "document.on.document.fill")
                            .resizable()
                            .frame(width: 14, height: 14)
                            .aspectRatio(contentMode: .fit)
                            .foregroundColor(Color("color_text_main"))
                        //                    }
                        
                        
                    }.frame(width: 160,height: 30)
                        .padding(.trailing,6)
                        .background(Color("color_wel_bg_01"))
                        .cornerRadius(15)
                        .shakeTapGes {
                            copyUserId()
                        }
                    
                }.frame(maxWidth: kScreenWidth)
                    .offset(y:-20)
            }
            .offset(y:10)
            
            //                .padding(.top,30)
        }
        
    }
    
    var listView:some View {
        VStack {
            
            ForEach(list,id: \.self) {item in
                if item["type"] == "push" {
                    //                    NavigationLinK
                    NavigationLink(value: item["title"]) {
                        settingRow(item: item)
                    }
                } else {
                    settingRow(item: item)
                        .onTapGesture {
                            if item["title"] == "分享App" {
                                shareApp()
                            } else if item["title"] == "建议反馈" {
                                giveSuggest()
                            } else if item["title"] == "给个好评" {
                                goodComment()
                            }
                        }
                }
                
            }
        }
        .padding(.vertical,10)
        .background(.white)
        .frame(width: kScreenWidth - 40)
        .cornerRadius(20)
        .offset(y:10)
    }
    
    private func copyUserId() {
        UIPasteboard.general.string = userId
        YWToast.show(text: "用户ID已复制")
    }
    
    private func shareApp() {
        
    }
    
    private func giveSuggest(){
        
    }
    
    private func goodComment() {
        
    }
    
    
    private func settingRow(item:[String:String]) -> some View {
        HStack {
            Image(systemName: item["icon"]!)
                .resizable()
                .frame(width: 16, height: 16)
                .aspectRatio(contentMode: .fill)
                .foregroundColor(.colorMain)
            
            Text(item["title"]!)
                .modifier(YWTextModifer(fontSize: 14, fontWeight: .medium, design: .rounded, color: Color("color_text_main")))
                .padding(.leading,6)
            Spacer()
            Image(systemName: "arrow.right")
                .resizable()
                .frame(width: 12, height: 12)
                .aspectRatio(contentMode: .fill)
                .foregroundColor(Color("color_text_sub"))
        }.padding(.horizontal,16)
            .padding(.vertical,16)
    }
    
    
}
